package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 目标:
 *
 * @author YUE
 * @date 2024/6/17 16:52
 */
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /*
      添加购物车
     */
    @Override
    public void save(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        // 条件判断  根据用户id查询购物车
        shoppingCart.setUserId(BaseContext.getCurrentId());
        // 1: 查询购物车是否包含相关 菜品 / 套餐
        List<ShoppingCart> list = shoppingCartMapper.userList(shoppingCart);

        if (list != null && list.size() > 0) {
            // 有相关菜品 / 套餐 直接数量 + 1
            // ① 获取这个记录
             shoppingCart = list.get(0);
            // ② 传参
            shoppingCartMapper.updateByNumber(shoppingCart.getNumber() + 1, shoppingCart.getId());
        } else {

            // 没有相关 菜品 / 套餐 向数据表中插入一条新数据
            // 判断是菜品 还是套餐 逐一添加
            if (shoppingCartDTO.getDishId() != null) {
                // 本次添加的是菜品 查询菜品表 , 获取菜品名称 图片路径 单价
                Dish dish = dishMapper.getById(shoppingCartDTO.getDishId());
                //封装购物车对象
                shoppingCart = ShoppingCart.builder()
                        .name(dish.getName())
                        .dishId(dish.getId())
                        .dishFlavor(shoppingCartDTO.getDishFlavor())
                        .amount(dish.getPrice())
                        .image(dish.getImage())
                        .build();
            } else {
                // 本次添加的是套餐 查询套餐表 , 获取套餐名称 图片路径 单价
                Setmeal setmeal = setmealMapper.getById(shoppingCartDTO.getSetmealId());
                //封装属性
                shoppingCart = ShoppingCart.builder()
                        .name(setmeal.getName())
                        .dishId(setmeal.getId())
                        .amount(setmeal.getPrice())
                        .image(setmeal.getImage())
                        .build();
            }

            // 提取公共代码
            shoppingCart.setUserId(BaseContext.getCurrentId());
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setNumber(1);
            shoppingCartMapper.inster(shoppingCart);
        }


    }
    /*
      查询购物车
     */
    @Override
    public List<ShoppingCart> showShoppingCart() {
        // 根据用户id查询购物车
        Long userId = BaseContext.getCurrentId();
       List<ShoppingCart> list = shoppingCartMapper.getByUserId(userId);
        return list;
    }


    /*
        清空购物车
     */
    @Override
    public void deldeteShoppingCart() {
        shoppingCartMapper.deleteShoppingCart(BaseContext.getCurrentId());
    }


    /*
      删除购物车中的一个商品
     */
    @Override
    public void deleteGoods(ShoppingCartDTO shoppingCartDTO) {
        // 查询该用户购物车 的数据
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());

        List<ShoppingCart> list = shoppingCartMapper.userList(shoppingCart);


       if(list !=null && list.size() > 0){
           shoppingCart = list.get(0);
               // 判断 此商品 数量 =1 / >1
               if (shoppingCart.getNumber() == 1){
                   // 根据购物车id直接删除
                   shoppingCartMapper.deleteGoods(shoppingCart.getId());
           }else {
               // > 1 数量 -1
               shoppingCartMapper.updateByNumber(shoppingCart.getNumber()-1,shoppingCart.getId());

           }
       }
    }
}